package com.zyl.pagergallery_java.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyl.pagergallery_java.R;
import com.zyl.pagergallery_java.pojo.Pixabay;

import uk.co.senab.photoview.PhotoView;

public class PhotoAdapter extends ListAdapter<Pixabay.PhotoItem, PhotoAdapter.PhotoViewHolder> {

    public PhotoAdapter() {
        super(new DiffUtil.ItemCallback<Pixabay.PhotoItem>() {
            @Override
            public boolean areItemsTheSame(@NonNull Pixabay.PhotoItem oldItem, @NonNull Pixabay.PhotoItem newItem) {
                return oldItem == newItem;
            }

            @Override
            public boolean areContentsTheSame(@NonNull Pixabay.PhotoItem oldItem, @NonNull Pixabay.PhotoItem newItem) {
                return oldItem.getPhotoId() == newItem.getPhotoId();
            }
        });
    }

    @NonNull
    @Override
    public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_photo, parent, false);
        PhotoViewHolder photoViewHolder = new PhotoViewHolder(inflate);
        return photoViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull PhotoViewHolder holder, int position) {
        Glide.with(holder.itemView)
                .load(getItem(position).getFullUrl())
                .placeholder(R.drawable.ic_baseline_photo_size_select_actual_24)
                .into(holder.photoView);
    }

    class PhotoViewHolder extends RecyclerView.ViewHolder {
        PhotoView photoView;

        public PhotoViewHolder(@NonNull View itemView) {
            super(itemView);
            photoView = itemView.findViewById(R.id.imageViewPhoto);
        }
    }
}
